# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=liblktlang
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=25.0.0
pkgrel=2
pkgdesc="A Python framework to generate language parsers (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url="https://github.com/AdaCore/langkit"
license=('spdx:Apache-2.0 WITH LLVM-exception')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-gcc-ada"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-core"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-gmp"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-iconv"
         "${MINGW_PACKAGE_PREFIX}-libvss"
         "${MINGW_PACKAGE_PREFIX}-prettier-ada")
makedepends=("${MINGW_PACKAGE_PREFIX}-gprbuild"
             "${MINGW_PACKAGE_PREFIX}-langkit"
             "${MINGW_PACKAGE_PREFIX}-langkit_support"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
options=("!strip")
source=("https://github.com/AdaCore/langkit/archive/v${pkgver}/langkit-${pkgver}.tar.gz"
        "https://github.com/AdaCore/AdaSat/archive/v${pkgver}/AdaSat-${pkgver}.tar.gz")
sha256sums=('de7976a815b810da98c841c475a0d0d83ed45dc1016c71327902ba63aa0d651c'
            'b823969345cdd3678bf3cb1eb55592a0f74c2d99fc67303e187b4fbc960188a2')

prepare() {
  [[ -d langkit-${pkgver}/langkit/adasat ]] && rm -rf langkit-${pkgver}/langkit/adasat
  cp -r "${srcdir}"/AdaSat-${pkgver} langkit-${pkgver}/langkit/adasat
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  cp -r "langkit-${pkgver}" "build-${MSYSTEM}" && cd "build-${MSYSTEM}"

  GPR_PROJECT_PATH="${srcdir}"/build-${MSYSTEM}/langkit/adasat \
  ${MINGW_PREFIX}/bin/python manage.py \
    make \
    --build-mode prod \
    --lib lkt \
    --no-langkit-support \
    --no-mypy \
    --library-types=static,static-pic,relocatable

  cd contrib/lkt/build/python
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"/contrib/lkt

  ${MINGW_PREFIX}/bin/python manage.py \
    install \
    "${pkgdir}"${MINGW_PREFIX} \
    --build-mode prod \
    --disable-all-mains \
    --library-types=static,static-pic,relocatable

  cd build/python
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 "${srcdir}"/langkit-${pkgver}/LICENSE.txt \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"
}
